<template>
  <div class="swiper-container" ref="cur">
    <div class="swiper-wrapper">
      <div
        class="swiper-slide"
        v-for="(carousel, index) in list"
        :key="carousel.id"
      >
        <img :src="carousel.imgUrl" width="1000px" height="600px" />
      </div>
    </div>
    <!-- 如果需要分页器 -->
    <div class="swiper-pagination"></div>
    <!-- 
    如果需要导航按钮
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div> -->
  </div>
</template>

<script>
import Swiper from "swiper";
export default {
  name: "Carousel",
  // 接收父组件传来的list
  props: ["list"],
  // 因为要将轮播组件封装起来以便后期方便使用,
  // 但是出现一个问题:floor中的轮播是在mouted挂载后就可以实现(因为数据是父组件传来的,不存在dom未加载好的情况),
  // 因此还是使用watch来监听list
  watch: {
    list: {
      // 因此打开immediate实时监控
      immediate: true,
      handler() {
        // 因为监听到的数据已经有了,但是v-for动态渲染结构还是没有办法确认
        this.$nextTick(() => {
          var mySwiper = new Swiper(this.$refs.cur, {
            loop: true,
            pagination: {
              el: ".swiper-pagination",
            },
            navigation: {
              nextEl: ".swiper-button-next",
              prevEl: ".swiper-button-prev",
            },
            clickable: true,
          });
        });
      },
    },
  },
  mounted() {
    // console.log(this.list);
  },
};
</script>

<style>
</style>